<el-dialog id="formMvcp" title="移动/复制到指定位置" :visible.sync="dialogMvcpVisible" width="600px" v-cloak >
    <span>当前选择:{{selectionFolder}}</span>
    <el-input
            placeholder="搜索文件夹"
            v-model="filterText"
            style="margin-top: 15px;">
    </el-input>
    <el-scrollbar class="ms-scrollbar" style="height: 100%;">
        <el-tree
                class="filter-tree"
                :data="folderData"
                node-key="fileId"
                :default-expanded-keys="['ROOT']"
                :highlight-current="true"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                @node-click="handleNodeClick"
                ref="treeFold"
                v-loading="loadingTree"
                style="max-height:200px;">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                    <#--:class="imgFlag?'file-img':'file-edit'-->
                    <span><i :class="node.expanded?'el-icon-folder-opened':'el-icon-folder'"  style="color:orange" ></i> {{ node.label }}</span>
                </span>
        </el-tree>
    </el-scrollbar>
    <div slot="footer">
        <el-button size="mini" type="primary" @click="mvcpConfirm('MV')">移 动</el-button>
        <el-button size="mini" type="primary" @click="mvcpConfirm('CP')">复 制</el-button>
    </div>
</el-dialog>
<script>
    var formMvcp = new Vue({
        el: '#formMvcp',
        data: function () {
            return {
                mvcpForm:{},
                dialogMvcpVisible:false,
                selectionFolder:'',
                defaultExpandedKeys:['ROOT'],
                filterText: '',
                folderData: [],
                loadingTree:false
            };
        },
        watch: {
            filterText:function(val) {
                this.$refs.treeFold.filter(val);
            }
        },
        computed: {},
        methods: {
            showMvcpDialog:function (row) {
                this.dialogMvcpVisible = true;
                this.getFolderData();
                this.mvcpForm={};
                this.selectionFolder = "";
                this.filterText = "";
                this.defaultExpandedKeys=["1"]
                this.mvcpForm.sourceFileId = row[0].id;
            },
            //获取目录列表
            getFolderData:function () {
                var that = this;
                that.loadingTree = true;
                ms.http.get(ms.manager + "/cms/document/dir/tree.do").then(function (res) {
                    if(res.code == 200){
                        that.folderData = res.data;
                        that.$nextTick(function () {
                            that.$refs.treeFold.setCurrentKey(null);
                        })
                    }
                }).finally(function () {
                    that.loadingTree = false;
                });
            },
            //移动复制
            mvcpConfirm:function(operateType){
                var that = this;
                var operateTypeStr = operateType==="MV"?"移动":operateType==="CP"?"复制":""
                var currentNode = this.$refs.treeFold.getCurrentNode();
                console.log("currentNode",currentNode)
                if(!currentNode){
                    that.$notify({
                        type: 'warning',
                        message: "请选择文件夹"
                    });
                    return;
                }
                that.mvcpForm.targetFileId = currentNode.fileId;
                that.mvcpForm.operateType = operateType;
                that.dialogMvcpVisible = false;
                ms.http.post(ms.manager + "/cms/document/mvcp.do", that.mvcpForm,{
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(function (res) {
                    if(res.code===200){
                        that.$notify({
                            type: 'success',
                            message: operateTypeStr+"成功"
                        });
                        //操作成功，刷新列表
                        that.dialogCollectVisible = false;
                        indexVue.list();
                    }else{
                        that.$notify({
                            type: 'error',
                            message: operateTypeStr+"失败"
                        });
                    }


                }).catch(function (err) {
                    that.$notify({
                        type: 'error',
                        message: operateTypeStr+"失败"
                    });
                });
            },
            handleNodeClick:function(data) {
                this.selectionFolder = data.label
            },
            filterNode:function(value, data, node) {
                if (!value) return true;
                return  this.chooseNode(value, data, node); // 调用过滤二层方法
            },
            chooseNode:function(value, data, node) {
                if (data.label.indexOf(value) !== -1) {
                    return true;
                }
                var level = node.level;
                // 如果传入的节点本身就是一级节点就不用校验了
                if (level === 1) {
                    return false;
                }
                // 先取当前节点的父节点
                var parentData = node.parent;
                // 遍历当前节点的父节点
                var index = 0;
                while (index < level - 1) {
                    // 如果匹配到直接返回
                    if (parentData.data.label.indexOf(value) !== -1) {
                        return true;
                    }
                    // 否则的话再往上一层做匹配
                    parentData = parentData.parent;
                    index++;
                }
                // 没匹配到返回false
                return false
            },
        },
        created: function () {}
    });
</script>
<style>

</style>
